using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;

namespace HeroldIT.Dnn.Modules.News
{
    internal static class SettingsHelper
    {
        #region constants

        #region INewsCoreSettingsProvider

        [DefaultValue(-1)]
        public const string KeyDefaultListTabId = "DefaultListTabId";

        [DefaultValue(-1)]
        public const string KeyDefaultDetailTabId = "DefaultDetailTabId";

        [DefaultValue("en-US")]
        public const string KeyDefaultLocale = "DefaultLocale";

        [DefaultValue(true)]
        public const string KeyDefaultShowOnlyNewsForSelectedLocale = "ShowOnlyNewsForSelectedLocale";

        [DefaultValue("Images/News[I]/Teaser")]
        public const string KeyTeaserImageFolder = "TeaserImageFolder";

        [DefaultValue(150)]
        public const string KeyMaxTeaserImageWidth = "MaxTeaserImageWidth";

        [DefaultValue(0)]
        public const string KeyMaxTeaserImageHeight = "MaxTeaserImageHeight";

        [DefaultValue("#ffffff")]
        public const string KeyTeaserImageMatte = "TeaserImageMatte";

        [DefaultValue(85L)]
        public const string KeyTeaserImageQuality = "TeaserImageQuality";

        [DefaultValue("Images/News[I]/Detail")]
        public const string KeyDetailImageFolder = "DetailImageFolder";

        [DefaultValue(0)]
        public const string KeyMaxDetailImageWidth = "MaxDetailImageWidth";

        [DefaultValue(0)]
        public const string KeyMaxDetailImageHeight = "MaxDetailImageHeight";

        [DefaultValue("#ffffff")]
        public const string KeyDetailImageMatte = "DetailImageMatte";

        [DefaultValue(85L)]
        public const string KeyDetailImageQuality = "DetailImageQuality";

        [DefaultValue(15)]
        public const string KeyMaxCurrentItems = "MaxCurrentItems";

        [DefaultValue(false)]
        public const string KeyUseDetailImageForTeaserDefault = "UseDetailImageForTeaserDefault";

        [DefaultValue(false)]
        public const string KeyShowDisabledLanguages = "ShowDisabledLanguages";

        [DefaultValue(1)]
        public const string KeyMaxImagesToUse = "MaxImagesToUse";

        #endregion

        #region INewsSatelliteModuleSettingsProvider

        [DefaultValue(-1)]
        public const string KeyNewsCoreModuleId = "NewsCoreModuleId";

        #endregion

        #region INewsListSettingsProvider

        [DefaultValue("[S]News/List.ascx")]
        public const string KeyListSkinUrl = "ListSkinUrl";

        [DefaultValue(-1)]
        public const string KeyDetailTabId = "DetailTabId";

        [DefaultValue(true)]
        public const string KeyShowCurrentItems = "ShowCurrentItems";

        [DefaultValue(false)]
        public const string KeyShowArchivedItems = "ShowArchivedItems";

        [DefaultValue(15)]
        public const string KeyMaxVisibleItems = "MaxVisibleItems";

        [DefaultValue(true)]
        public const string KeyShowOnlyNewsForSelectedLocale = "ShowOnlyNewsForSelectedLocale";

        [DefaultValue("")]
        public const string KeyCategoryFilter = "CategoryFilter";

        [DefaultValue(false)]
        public const string KeyAddFeedToPage = "AddFeedToPage";

        #endregion

        #region INewsDetailSettingsProvider

        [DefaultValue("[S]News/Detail.ascx")]
        public const string KeyDetailSkinUrl = "DetailSkinUrl";

        [DefaultValue(-1)]
        public const string KeyListTabId = "ListTabId";

        #endregion

        #endregion

        #region static methods

        #region INewsCoreSettingsProvider

        public static int GetDefaultListTabId(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof (SettingsHelper), "KeyDefaultListTabId");
        }

        public static void SetDefaultListTabId(this INewsCoreSettingsProvider module, int value)
        {
            module.SetSetting(KeyDefaultListTabId, value.ToString(CultureInfo.InvariantCulture));
        }

        public static int GetDefaultDetailTabId(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof (SettingsHelper), "KeyDefaultDetailTabId");
        }

        public static void SetDefaultDetailTabId(this INewsCoreSettingsProvider module, int value)
        {
            module.SetSetting(KeyDefaultDetailTabId, value.ToString(CultureInfo.InvariantCulture));
        }

        public static string GetDefaultLocale(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault(typeof(SettingsHelper), "KeyDefaultLocale");
        }

        public static void SetDefaultLocale(this INewsCoreSettingsProvider module, string value)
        {
            module.SetSetting(KeyDefaultLocale, value);
        }

        public static bool GetDefaultShowOnlyNewsForSelectedLocale(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<bool>(typeof(SettingsHelper), "KeyDefaultShowOnlyNewsForSelectedLocale");
        }

        public static void SetDefaultShowOnlyNewsForSelectedLocale(this INewsCoreSettingsProvider module, bool value)
        {
            module.SetSetting(KeyDefaultShowOnlyNewsForSelectedLocale, value.ToString(CultureInfo.InvariantCulture));
        }

        public static string GetTeaserImageFolder(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault(typeof(SettingsHelper), "KeyTeaserImageFolder");
        }

        public static void SetTeaserImageFolder(this INewsCoreSettingsProvider module, string value)
        {
            module.SetSetting(KeyTeaserImageFolder, value);
        }

        public static int GetMaxTeaserImageWidth(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof(SettingsHelper), "KeyMaxTeaserImageWidth");
        }

        public static void SetMaxTeaserImageWidth(this INewsCoreSettingsProvider module, int value)
        {
            module.SetSetting(KeyMaxTeaserImageWidth, value.ToString(CultureInfo.InvariantCulture));
        }

        public static int GetMaxTeaserImageHeight(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof(SettingsHelper), "KeyMaxTeaserImageHeight");
        }

        public static void SetMaxTeaserImageHeight(this INewsCoreSettingsProvider module, int value)
        {
            module.SetSetting(KeyMaxTeaserImageHeight, value.ToString(CultureInfo.InvariantCulture));
        }

        public static string GetTeaserImageMatte(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault(typeof(SettingsHelper), "KeyTeaserImageMatte");
        }

        public static Color GetTeaserImageMatteColor(this INewsCoreSettingsProvider module)
        {
            try
            {
                return ColorTranslator.FromHtml(module.GetTeaserImageMatte());
            }
            catch (Exception)
            {
                return Color.White;
            }
        }

        public static void SetTeaserImageMatte(this INewsCoreSettingsProvider module, string value)
        {
            module.SetSetting(KeyTeaserImageMatte, value);
        }

        public static long GetTeaserImageQuality(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<long>(typeof(SettingsHelper), "KeyTeaserImageQuality");
        }

        public static void SetTeaserImageQuality(this INewsCoreSettingsProvider module, long value)
        {
            module.SetSetting(KeyTeaserImageQuality, value.ToString(CultureInfo.InvariantCulture));
        }

        public static string GetDetailImageFolder(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault(typeof(SettingsHelper), "KeyDetailImageFolder");
        }

        public static void SetDetailImageFolder(this INewsCoreSettingsProvider module, string value)
        {
            module.SetSetting(KeyDetailImageFolder, value);
        }

        public static int GetMaxDetailImageWidth(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof(SettingsHelper), "KeyMaxDetailImageWidth");
        }

        public static void SetMaxDetailImageWidth(this INewsCoreSettingsProvider module, int value)
        {
            module.SetSetting(KeyMaxDetailImageWidth, value.ToString(CultureInfo.InvariantCulture));
        }

        public static int GetMaxDetailImageHeight(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof(SettingsHelper), "KeyMaxDetailImageHeight");
        }

        public static void SetMaxDetailImageHeight(this INewsCoreSettingsProvider module, int value)
        {
            module.SetSetting(KeyMaxDetailImageHeight, value.ToString(CultureInfo.InvariantCulture));
        }

        public static string GetDetailImageMatte(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault(typeof(SettingsHelper), "KeyDetailImageMatte");
        }

        public static Color GetDetailImageMatteColor(this INewsCoreSettingsProvider module)
        {
            try
            {
                return ColorTranslator.FromHtml(module.GetDetailImageMatte());
            }
            catch (Exception)
            {
                return Color.White;
            }
        }

        public static void SetDetailImageMatte(this INewsCoreSettingsProvider module, string value)
        {
            module.SetSetting(KeyDetailImageMatte, value);
        }

        public static long GetDetailImageQuality(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<long>(typeof(SettingsHelper), "KeyDetailImageQuality");
        }

        public static void SetDetailImageQuality(this INewsCoreSettingsProvider module, long value)
        {
            module.SetSetting(KeyDetailImageQuality, value.ToString(CultureInfo.InvariantCulture));
        }

        public static int GetMaxCurrentItems(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof(SettingsHelper), "KeyMaxCurrentItems");
        }

        public static void SetMaxCurrentItems(this INewsCoreSettingsProvider module, int value)
        {
            if (value < 1)
            {
                value = 1;
            }
            module.SetSetting(KeyMaxCurrentItems, value.ToString(CultureInfo.InvariantCulture));
        }

        public static bool GetUseDetailImageForTeaserDefault(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<bool>(typeof (SettingsHelper), "KeyUseDetailImageForTeaserDefault");
        }

        public static void SetUseDetailImageForTeaserDefault(this INewsCoreSettingsProvider module, bool value)
        {
            module.SetSetting(KeyUseDetailImageForTeaserDefault, value.ToString(CultureInfo.InvariantCulture));
        }

        public static bool GetShowDisabledLanguages(this INewsCoreSettingsProvider module)
        {
            return module.GetSettingOrDefault<bool>(typeof (SettingsHelper), "KeyShowDisabledLanguages");
        }

        public static void SetShowDisabledLanguages(this INewsCoreSettingsProvider module, bool value)
        {
            module.SetSetting(KeyShowDisabledLanguages, value.ToString(CultureInfo.InvariantCulture));
        }

        public static int GetMaxImagesToUse(this INewsCoreSettingsProvider module)
        {
            return Math.Max(1, Math.Min(4, module.GetSettingOrDefault<int>(typeof (SettingsHelper), "KeyMaxImagesToUse")));
        }

        public static void SetMaxImagesToUse(this INewsCoreSettingsProvider module, int value)
        {
            module.SetSetting(KeyMaxImagesToUse, value.ToString(CultureInfo.InvariantCulture));
        }


        #endregion

        #region INewsSatelliteModuleSettingsProvider

        public static int GetNewsCoreModuleId(this INewsSatelliteModuleSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof (SettingsHelper), "KeyNewsCoreModuleId");
        }

        public static void SetNewsCoreModuleId(this INewsSatelliteModuleSettingsProvider module, int value)
        {
            module.SetSetting(KeyNewsCoreModuleId, value.ToString(CultureInfo.InvariantCulture));
        }

        #endregion

        #region INewsListSettingsProvider

        public static string GetListSkinUrl(this INewsListSettingsProvider module)
        {
            return module.GetSettingOrDefault(typeof (SettingsHelper), "KeyListSkinUrl");
        }

        public static void SetListSkinUrl(this INewsListSettingsProvider module, string value)
        {
            module.SetSetting(KeyListSkinUrl, value);
        }

        public static int GetDetailTabId(this INewsListSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof(SettingsHelper), "KeyDetailTabId");
        }

        public static void SetDetailTabId(this INewsListSettingsProvider module, int value)
        {
            module.SetSetting(KeyDetailTabId, value.ToString(CultureInfo.InvariantCulture));
        }

        public static bool GetShowCurrentItems(this INewsListSettingsProvider module)
        {
            return module.GetSettingOrDefault<bool>(typeof(SettingsHelper), "KeyShowCurrentItems");
        }

        public static void SetShowCurrentItems(this INewsListSettingsProvider module, bool value)
        {
            module.SetSetting(KeyShowCurrentItems, value.ToString(CultureInfo.InvariantCulture));
        }

        public static bool GetShowArchivedItems(this INewsListSettingsProvider module)
        {
            return module.GetSettingOrDefault<bool>(typeof(SettingsHelper), "KeyShowArchivedItems");
        }

        public static void SetShowArchivedItems(this INewsListSettingsProvider module, bool value)
        {
            module.SetSetting(KeyShowArchivedItems, value.ToString(CultureInfo.InvariantCulture));
        }

        public static int GetMaxVisibleItems(this INewsListSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof(SettingsHelper), "KeyMaxVisibleItems");
        }

        public static void SetMaxVisibleItems(this INewsListSettingsProvider module, int value)
        {
            if (value < 1)
            {
                value = 1;
            }
            module.SetSetting(KeyMaxVisibleItems, value.ToString(CultureInfo.InvariantCulture));
        }

        public static bool GetShowOnlyNewsForSelectedLocale(this INewsListSettingsProvider module)
        {
            return module.GetSettingOrDefault<bool>(typeof (SettingsHelper), "KeyShowOnlyNewsForSelectedLocale");
        }

        public static void SetShowOnlyNewsForSelectedLocale(this INewsListSettingsProvider module, bool value)
        {
            module.SetSetting(KeyShowOnlyNewsForSelectedLocale, value.ToString(CultureInfo.InvariantCulture));
        }

        public static ICollection<string> GetCategoryFilter(this INewsListSettingsProvider module)
        {
            string categoryFilter = module.GetSettingOrDefault<string>(typeof(SettingsHelper), "KeyCategoryFilter");
            if (String.IsNullOrEmpty(categoryFilter))
            {
                return null;
            }
            return categoryFilter.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static void SetCategoryFilter(this INewsListSettingsProvider module, ICollection<string> value)
        {
            if (null == value || value.Count <= 0)
            {
                module.SetSetting(KeyCategoryFilter, null);
            }
            else
            {
                module.SetSetting(KeyCategoryFilter, String.Join(";", value.Where(v => !String.IsNullOrEmpty(v)).ToArray()));
            }
        }

        public static bool GetAddFeedToPage(this INewsListSettingsProvider module)
        {
            return module.GetSettingOrDefault<bool>(typeof (SettingsHelper), "KeyAddFeedToPage");
        }

        public static void SetAddFeedToPage(this INewsListSettingsProvider module, bool value)
        {
            module.SetSetting(KeyAddFeedToPage, value.ToString(CultureInfo.InvariantCulture));
        }

        #endregion

        #region INewsDetailSettingsProvider

        public static string GetDetailSkinUrl(this INewsDetailSettingsProvider module)
        {
            return module.GetSettingOrDefault(typeof (SettingsHelper), "KeyDetailSkinUrl");
        }

        public static void SetDetailSkinUrl(this INewsDetailSettingsProvider module, string value)
        {
            module.SetSetting(KeyDetailSkinUrl, value);
        }

        public static int GetListTabId(this INewsDetailSettingsProvider module)
        {
            return module.GetSettingOrDefault<int>(typeof(SettingsHelper), "KeyListTabId");
        }

        public static void SetListTabId(this INewsDetailSettingsProvider module, int value)
        {
            module.SetSetting(KeyListTabId, value.ToString(CultureInfo.InvariantCulture));
        }

        #endregion

        #endregion
    }
}